using System.IO;
using UnityEditor;
using UnityEngine;

public class AssemblyEditorWindow : EditorWindow
{
    static AssemblyEditorWindow window;
    GameObject selectedObject;
    string filePath;
    AllStep allStep;
    StepInfo stepInfo;
    PartInfo partInfo;

    [MenuItem("工具/拆装工具", false, 0)]
    static void Init()
    {
        window = (AssemblyEditorWindow)EditorWindow.GetWindow(typeof(AssemblyEditorWindow), false, "拆装工具", true);
        window.Show();
    }

    private void OnGUI()
    {
        EditorGUILayout.Space();
        if (GUILayout.Button("选择步骤文件"))
        {
            filePath = EditorUtility.OpenFilePanel("选择打开文件", Application.streamingAssetsPath, "json");
            allStep = JsonUtility.FromJson<AllStep>(System.IO.File.ReadAllText(filePath));
        }
        if (allStep == null)
        {
            return;
        }
        EditorGUILayout.Space();
        allStep.title = EditorGUILayout.TextField("设备标题", allStep.title);
        EditorGUILayout.Space();
        selectedObject = (GameObject)EditorGUILayout.ObjectField("选择物体", selectedObject, typeof(GameObject), true);
        EditorGUILayout.Space();

        if (selectedObject != null)
        {
            EditorGUILayout.LabelField("按键1记录初始位置和旋转");
            EditorGUILayout.Space();

            //按键1记录初始位置和旋转
            if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha1)
            {
                stepInfo = new StepInfo();
                partInfo = new PartInfo();
                partInfo.modelName = selectedObject.name;
                partInfo.SetPosFrom(selectedObject.transform.localPosition);
                partInfo.SetRotFrom(TransformUtils.GetInspectorRotation(selectedObject.transform));
                //Repaint();
            }
            if (partInfo == null)
            {
                return;
            }
            EditorGUILayout.LabelField("初始位置:" + partInfo.posFrom.ToString());
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("初始旋转:" + partInfo.rotFrom.ToString());
            EditorGUILayout.Space();

            EditorGUILayout.LabelField("按键2记录目标位置和旋转");
            EditorGUILayout.Space();

            //按键2记录目标位置和旋转
            if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Alpha2)
            {
                partInfo.modelName = selectedObject.name;
                partInfo.SetPosTo(selectedObject.transform.localPosition);
                partInfo.SetRotTo(TransformUtils.GetInspectorRotation(selectedObject.transform));
                //Repaint();
            }
            EditorGUILayout.LabelField("目标位置:" + partInfo.posTo.ToString());
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("目标旋转:" + partInfo.rotTo.ToString());
            EditorGUILayout.Space();

            if (GUILayout.Button("保存步骤"))
            {
                stepInfo.partInfos.Add(partInfo);
                allStep.stepInfos.Add(stepInfo);
                string jsonStr = JsonUtility.ToJson(allStep, true);
                Debug.Log("jsonStr:" + jsonStr);
                File.WriteAllText(filePath, jsonStr);
            }
        }
    }
}